#include "stdafx.h"
#include "vLabel.h"

namespace vview
{

IMPLEMENT_RUNTIMEINFO(CVLabel, CVView)

void CVLabel::SetText( const wstring& strCaption )
{
	if (m_Text != strCaption)
	{
		m_Text = strCaption;
		CheckAutoSize(m_Width);
		DoNotify(NTYPE_TEXTCHANGED, NULL);
	}
}

void CVLabel::SetWrap( bool bWrap )
{
	if (m_bWrap != bWrap)
	{
		m_bWrap = bWrap;
		CheckAutoSize(m_Width);
	}
}

void CVLabel::SetTextAlign( TextAlign align )
{
	if (m_TextAlign != align)
	{
		m_TextAlign = align;
		CheckAutoSize(m_Width);
	}
}

void CVLabel::SetAutoSize( bool bAutoSize )
{
	if (bAutoSize != m_bAutoSize)
	{
		m_bAutoSize = bAutoSize;
		CheckAutoSize(m_Width);
	}
}

void CVLabel::SetTextTrimming(TextTrimming endType)
{
	if (endType != m_TextTrimming)
	{
		m_TextTrimming = endType;
		CheckAutoSize(m_Width);
	}
}

void CVLabel::CheckAutoSize(int nWidth)
{
	if (m_bAutoSize && (m_BlockCount == 0))
	{
		SIZE sz = GetTextSize(nWidth);
		SetSize(sz.cx, sz.cy);
	}
}

void CVLabel::DoNotify( NotifyType nType, DWORD dwParam )
{
	if (nType == NTYPE_SIZECHANGING)
	{
		if (m_bAutoSize && (m_BlockCount == 0))
		{
			SIZE* psz = (SIZE*)dwParam;
			*psz = GetTextSize(psz->cx);
			return;
		}
	}

	CVView::DoNotify(nType, dwParam);
}

void CVLabel::EndBlock()
{
	if (m_BlockCount > 0)
	{
		if (--m_BlockCount == 0)
			CheckAutoSize(m_Width);
	}
}
}